<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :visible="isActive" :title="title" @close="abolish">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDept" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="chaeckmanager">
          <el-option
            v-for="item in meployess"
            :key="item.value"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="Closes">确定</el-button>
        <el-button size="small" @click="abolish">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { Getdepartment, addDepartment, getDepartmentDetail, editDepartment } from '@/api/departments'
import { getSimple } from '@/api/employees'
export default {
  props: {
    isActive: {
      type: Boolean,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, val, callback) => {
      // 算法 数组高阶函数
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = this.depts
          .filter(item => item.pid === this.nodeData.pid)
          .some(item => item.name === val && item.id !== this.nodeData.id)
      } else {
        // 新增
        isRepeat = this.depts
          .filter(item => item.pid === this.nodeData.id)
          .some(item => item.name === val)
      }

      // .filter(item => item.pid === this.nodeData.id)
      // .some(item => item.name === val)
      console.log(isRepeat)
      //   console.log(this.depts
      //     .filter(item => item.pid === this.nodeData.id)
      //     .some(item => item.name === val))
      //   const isRepeat = true
      if (isRepeat) {
        callback(new Error('同一部门下不能重名'))
      } else {
        callback()
      }
    }
    const checkCodeRepeat = (rule, val, callback) => {
      // 检查是否有重复的 code
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = this.depts.some(item => item.code === val && item.id !== this.nodeData.id)
      } else {
        // 新增
        isRepeat = this.depts.some(item => item.code === val)
      }

      // 后续处理报错
      isRepeat ? callback(new Error('部门编码不能一样')) : callback()
    }
    return {
      formData: {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: '', // 部门名称
        pid: '' // 父级部门ID

      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { max: 50, message: '部门名称不能大于50字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { max: 50, message: '部门编码不能大于50字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        manager: [{ required: true, message: '负责人名称不能为空' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { max: 300, message: '部门介绍不能大于300字符', trigger: 'blur' }
        ]
      },
      depts: [],
      meployess: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  async created() {
    const { depts } = await Getdepartment()
    this.depts = depts
    this.meployess = await getSimple()
    // console.log(this.meployess)
    // console.log(123, this.depts)
  },
  methods: {
    async department() {
      const { depts } = await Getdepartment()
      this.depts = depts
    },
    chaeckmanager() {
      this.$refs.addDept.validateField('manager')
    },
    async Closes() {
      // 验证
      await this.$refs.addDept.validate()
      // 发请求
      if (this.formData.id) {
        // 编辑
        await editDepartment(this.formData)
      } else {
        // 新增
        await addDepartment({
          ...this.formData,
          pid: this.nodeData.id
        })
      }

      // 提示用户
      this.$message.success('成功添加部门')
      // 关闭窗口
      this.$emit('update:isActive', false)
      this.department()
      // 更新页面
      this.$emit('addDepartment')
    },
    abolish() {
      // 为了避免为完整清空，直接表单回复原样
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: '', // 部门名称
        pid: '' // 父级部门ID
      }
      this.$refs.addDept.resetFields()
      this.$emit('update:isActive', false)
    },
    async getDepartmentDetail(id) {
      console.log(id)
      this.formData = await getDepartmentDetail(id)
    }
  }

}
</script>

<style>

</style>
